/**
 * action
 * action就是用来告诉我们的状态管理器需要做什么样的一种操作。
 * 包含type、data或者还有其他元素的对象。
 */

import * as actionType from './action-type';
import { JSONObject } from '../../seats/schema/common';
import { TaskInfo } from '../../seats/schema/Task';
import { getSystemConfig, getSysUserInfo } from 'admin/servers/servers';

// 设置uniqueToken的值
export const setUniqueToken = (token: string) => {
  return {
    type: actionType.SET_UNIQUE_TOKEN,
    uniqueToken: token,
  };
};

// 设置encryptToken的值
export const setEncryptToken = (token: string) => {
  return {
    type: actionType.SET_ENCRYPT_TOKEN,
    encryptToken: token,
  };
};

// 设置token的值
export const setToken = (token: string) => {
  return {
    type: actionType.SET_TOKEN,
    token: token,
  };
};

// 设置用户登录信息，根据登录接口返回
export const setLoginInfo = (loginInfo: JSONObject) => {
  return {
    type: actionType.SET_LOGIN_INFO,
    loginInfo,
  };
};

//异步操作 设置用户信息的的值
export const setUserInfoLater = () => {
  return (dispatch: any) => {
    getSysUserInfo()
      .then(result => {
        dispatch(setUserInfo(result.data || {}));
      })
      .catch(err => {
        dispatch(setUserInfo({}));
      });
  };
};

// 设置用户user信息，根据查询用户信息接口返回
export const setUserInfo = (userInfo: JSONObject) => {
  return {
    type: actionType.SET_USER_INFO,
    userInfo,
  };
};

// 设置web 配置的值的值
export const setSystemConfig = (systemConfig: JSONObject) => {
  return {
    type: actionType.SET_SYSTEM_CONFIG,
    systemConfig,
  };
};

//异步操作 设置web配置的值的值
export const setSystemConfigAsynic = () => {
  return (dispatch: any) => {
    getSystemConfig()
      .then(result => {
        dispatch(setSystemConfig(result.data || {}));
        setTimeout(() => {
          document.title = (result.data || {}).systemName;
        }, 0);
      })
      .catch(err => {
        dispatch(setSystemConfig({}));
      });
  };
};

// 设置checkInState的值
export const setCheckInState = (checkInState: number) => {
  return {
    type: actionType.SET_CHECK_IN_STATE,
    checkInState,
  };
};

// 设置uaConfig的值
export const setUaConfig = (uaConfig: JSONObject) => {
  return {
    type: actionType.SET_UA_CONFIG,
    uaConfig,
  };
};

// 设置工作台workspaceInfo的值
export const setWorkspaceInfo = (workspaceInfo: TaskInfo | {}) => {
  return {
    type: actionType.SET_WORKSPACE_INFO,
    workspaceInfo,
  };
};

// 存储当前人工监听callId
export const setManListenCallId = (manListenCallId: string) => {
  return {
    type: actionType.SET_MANLISTEN_CALLID,
    manListenCallId,
  };
};

// 坐席自动接听开关，on-开启，off-取消
export const setAutoAnswer = (autoAnswer: string) => {
  return {
    type: actionType.SET_AUTO_ANSWER,
    autoAnswer,
  };
};
// 坐席小休开关，on-开启，off-取消
export const setSeatRest = (seatRest: string) => {
  return {
    type: actionType.SET_SEAT_REST,
    seatRest: seatRest,
  };
};
// 根据screenNum，设置弹屏box窗口
export const setScreenNum = (screenNum: number) => {
  return {
    type: actionType.SET_SCREEN_NUM,
    screenNum,
  };
};

// 设置录音模版Id
export const setSoundTemplateId = (soundTemplateId: string) => {
  return {
    type: actionType.SET_SOUND_TEMPLATE_ID,
    soundTemplateId,
  };
};
// 新建任务表单缓存
export const setAddTaskFormData = (addTaskFormData: JSONObject) => {
  return {
    type: actionType.SET_ADD_TASK_FORM_DATA,
    addTaskFormData,
  };
};

// 机器人外呼任务正在被截听的通话Id
export const setInterceptCallId = (interceptCallId: string) => {
  return {
    type: actionType.SET_INTERCEPT_CALL_ID,
    interceptCallId,
  };
};
